const Biz = require('../biz/index');
const DB = require('../db/index');
const exception = require('../exception');
const fw = require('../core/framework');
const camera = require('../component/camera');

function setDeviceTimestamp(successFn,errorFn){
    Biz.Config.save({code:'device',value:new Date().getTime(),name:'设备变化时间戳',remark:'识别设备是否有变化'},function(){
        if(fw.isFn(successFn))successFn();
    },function(){
        if(fw.isFn(errorFn))errorFn();
    })
}


module.exports = {
    init(router,urlencodedParser){
        router.post('/device/create', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                let deviceData = {
                    name:param.name,
                    code:param.code,
                    remark:param.remark,
                    merchant_id:fw.parseMerchantId(req)
                };
                Biz.Device.create(deviceData,(deviceDBRes)=>{
                    let alarmContactGroupDeviceData = {
                        alarm_contact_group_id:param.alarm_contact_group_id,
                        device_id:deviceDBRes.data.id,
                        merchant_id:fw.parseMerchantId(req)
                    };
                    Biz.AlarmContactGroupDevice.create(alarmContactGroupDeviceData,(alarmContactGroupDeviceDBRes)=>{
                        setDeviceTimestamp(function(){
                            res.send(deviceDBRes);
                        },function(){
                            res.send(deviceDBRes);
                        });
                    },(data)=>{
                        res.send(data);
                    });
                },(deviceDBRes)=>{
                    res.send(deviceDBRes);
                });
            });
        });
        router.post('/device/update', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    Biz.Device.update(param,(data)=>{
                        DB.AlarmContactGroupDevice.update({
                            alarm_contact_group_id:param.alarm_contact_group_id,
                            device_id:param.id
                        },'device_id').then(()=>{
                            setDeviceTimestamp(function(){
                                res.send(data);
                            },function(){
                                res.send(data);
                            });
                        });
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/device/delete', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                console.log('执行删除账户业务');
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    Biz.Device.delete(param,(data)=>{
                        DB.AlarmContactGroupDevice.delete({'device_id':param.id},'device_id').then(()=>{
                            setDeviceTimestamp(function(){
                                res.send(data);
                            },function(){
                                res.send(data);
                            });
                        });
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/device/read', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExit(param.id)){
                    // param.merchant_id = fw.parseMerchantId(req);
                    Biz.Device.read(param,(data)=>{
                        res.send(data);
                    },(data)=>{
                        res.send(data);
                    });
                }else{
                    res.send(exception.paramEx('id'));
                }
            });
        });
        router.post('/device/camera', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                if(fw.isExitAll(param,['id','code']).pass){
                    param.merchant_id = fw.parseMerchantId(req);
                    camera.takingPicture(param,function(path){
                        if(path){
                            res.send({code:0,data:path});
                        }else{
                            res.send(exception['1500']);
                        }
                    });
                    // Biz.Device.read(param,(data)=>{
                    //     res.send(data);
                    // },(data)=>{
                    //     res.send(data);
                    // });
                }else{
                    res.send(exception.paramEx(fw.isExitAll(param,['id','code']).data.join(',')));
                }
            });
        });
        router.post('/device/search', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                param.merchant_id = fw.parseMerchantId(req);
                Biz.Device.search(param,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
        router.post('/device/searchAll', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                param.merchant_id = fw.parseMerchantId(req);
                Biz.Device.searchAll(param,(data)=>{
                    res.send(data);
                },(data)=>{
                    res.send(data);
                });
            });
        });
        // let reqCount = 0;
        router.post('/device/useByAi/search', urlencodedParser, function(req, res) {
            // reqCount++;
            let param = fw.utils.parseBody(req);
            // console.log('====收到第'+reqCount+'次请求====');
            // console.log('收到请求的原数据：',req.body);
            // console.log('解析后的数据：',param);
            Biz.Config.hasNewDevice({code:'device',value:param.timestamp},function(timeRes){
                Biz.Device.searchAllToAI((data)=>{
                    res.send({
                        code:0,
                        data:{
                            timestamp:timeRes.value,
                            content:data.data
                        }
                    });
                },(data)=>{
                    res.send(data);
                });
            },function(){
                res.send(exception['1000']);
            })
        });

        router.post('/device/reportUser/search', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                let reportUserId = fw.parseUserId(req);
                param.merchant_id = fw.parseMerchantId(req);
                // 根据reportUserId查询 报告组和报告用户关系，根据报告组查询设备

            });
        });

        router.post('/device/camera', urlencodedParser, function(req, res) {
            fw.checkLogin(req,res,function(){
                let param = fw.utils.parseBody(req);
                param.merchant_id = fw.parseMerchantId(req);
                camera.init(param,function(){

                });
            });
        });
    }
};
